package com.dream.dinner;


import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	DisplayImageOptions options;
	ImageLoader imageLoader;
	String[] imageUrls={"http://t1.gstatic.com/images?q=tbn:ANd9GcSDhRvmQO_B_2klsa4hws6JLtfpTr2JIaRhVPJhO-j2vIqOXhl8Ra9pzTc"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	ActionBar actionbar = getActionBar();
    	actionbar.setHomeButtonEnabled(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
		.threadPriority(Thread.NORM_PRIORITY - 2)
		.denyCacheImageMultipleSizesInMemory()
		.discCacheFileNameGenerator(new Md5FileNameGenerator())
		.tasksProcessingOrder(QueueProcessingType.LIFO)
		.writeDebugLogs() // Remove for release app
		.build();
        options = new DisplayImageOptions.Builder()
      		.showImageOnLoading(R.drawable.ic_launcher)
      		.showImageForEmptyUri(R.drawable.ic_launcher)
      		.showImageOnFail(R.drawable.ic_launcher)
      		.cacheInMemory(true)
      		.cacheOnDisc(true)
      		.considerExifParams(true)
      		.displayer(new RoundedBitmapDisplayer(20))
      		.build();
        ImageLoader.getInstance().init(config);
        imageLoader=ImageLoader.getInstance();
        ListView listView = (ListView) findViewById(R.id.listView1);
		((ListView) listView).setAdapter(new ItemAdapter());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    class ItemAdapter extends BaseAdapter {
		private class ViewHolder {
			public ImageView image;
		}

		@Override
		public int getCount() {
			return imageUrls.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View view = convertView;
			final ViewHolder holder;
			if (convertView == null) {
				view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false);
				holder = new ViewHolder();
				holder.image = (ImageView) view.findViewById(R.id.image);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			

			imageLoader.displayImage(imageUrls[position], holder.image, options);

			return view;
		}
		

		
	}
}
